﻿using Project.Yan.Net.DAL;
using Project.Yan.Net.Redis.CacheModel;
using Project.Yan.Net.Redis.RedisBase;

namespace Project.Yan.Net.Redis.Helper
{
    public class UserCacheHelper : BaseCacheHelper<UserCacheModel,int>
    {
        private readonly IUserRepository _userRepository;
        private readonly IRedisRepository _redisRepository;

        public UserCacheHelper(IUserRepository userRepository, IRedisRepository redisRepository)
        {
            _userRepository = userRepository;
            _redisRepository = redisRepository; 
        }
        /// <summary>
        /// 
        /// </summary>
        protected override IRedisRepository RedisRepository => _redisRepository;

        protected override async Task<UserCacheModel> BuildDataByDb(int key)
        {
            var result = await _userRepository.GetByIdAsync(key);
            return new UserCacheModel() { Id = result.Id, Username = result.Username, Email = result.Email };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected override string GetCacheKeyFromObjKey(int key)
        {
            if (key == 0) return string.Empty;
            return "Model:UserModelInfo:" + key;
        }
    }
}
